<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class Agent extends Model
{
    protected $table = "c_agent";

    public function province()
    {
        return $this->hasOne('App\Models\Area', 'id', 'province_id');
    }

    public function city()
    {
        return $this->hasOne('App\Models\Area', 'id', 'city_id');
    }

    public function district()
    {
        return $this->hasOne('App\Models\Area', 'id', 'district_id');
    }



    /***
     * @param $data
     * 申请供应商
     */
    public function addSuppliers($data)
    {
        try {
            DB::beginTransaction(); // 开始事务
            if (isset($data['id']) && !empty($data['id'])) { // 更新
                $has = (new Supplier())->where([['account', '=', $data['account']], ['id', '!=', $data['id']]])->first();
                if (!empty($has)) {
                    return "改账号已存在不能重复注册!";
                }
                $redata = $this->getInfo($data['id']);
                $redata->user_id = $data['user_id'];
                $redata->account = isset($data['account']) ? $data['account'] : $redata->account;
                $redata->pass = isset($data['pass']) ? bcrypt($data['pass']) : $redata->pass;
                $redata->user_name = isset($data['user_name']) ? $data['user_name'] : $redata->user_name;
                $redata->phone = isset($data['phone']) ? $data['phone'] : $redata->phone;
                $redata->front_card = isset($data['front_card']) ? $data['front_card'] : $redata->front_card;
                $redata->behind_card = isset($data['behind_card']) ? $data['behind_card'] : $redata->behind_card;
                $redata->company = isset($data['company']) ? $data['company'] : $redata->company;
                $redata->license_img = isset($data['license_img']) ? $data['license_img'] : $redata->license_img;
                $redata->remark = isset($data['remark']) ? $data['remark'] : $redata->remark;
                $redata->status = 0;
                $res = $redata->save();
            } else {
                $has = $this->getSupplierAccount($data['account']);
                if (!empty($has)) {
                    return "改账号已存在不能重复注册!";
                }
                //添加
                $this->user_id = $data['user_id'];
                $this->account = $data['account'];
                $this->pass = bcrypt($data['pass']);
                $this->user_name = $data['user_name'];
                $this->phone = $data['phone'];
                $this->front_card = $data['front_card'];
                $this->behind_card = $data['behind_card'];
                $this->company = $data['company'];
                $this->license_img = $data['license_img'];
                $this->remark = isset($data['remark']) ? $data['remark'] : "";
                $res = $this->save();
            }
            if ($res) {
                DB::commit(); // 提交事务
                return ['supplier_id' => $this->id ?: $data['id']];
            } else {
                return '入驻失败';
                DB::rollback(); // 回滚事务
            }
        } catch (Exception $e) {
            DB::rollback(); // 回滚事务
        }
    }

    /***
     * @param $data
     * @return mixed
     * 查看账号是否存在
     */
    public function getSupplierAccount($wid)
    {
        return $this->where("account", '=', $wid)->first();
    }

    /**
     * @param $id
     * @return mixed
     * 查询供应商信息
     */
    public function getInfo($id)
    {
        return $this->where([['id', '=', $id]])->first();
    }
}
